const express = require("express");
const expressJWT = require("express-jwt");
const jwt = require("jsonwebtoken");
const cors = require("cors");
const path = require("path")
const app = express();

const connectDB = require("./db/mongoDB");
const loginRouter = require("./router/login");
const getTitle = require('./router/title');
// const getQiniuToken = require('./router/qiniuToken');
const find = require('./router/find');
const port = 3011;
const upload = require('./uploadFile/upload')
const cards = require('./router/card');
const user = require('./router/user');
const code = require('./middleware/code');
const search = require('./router/search');
const carouselController = require('./controllers/carouselController')
const comment = require('./router/comment');
const register = require('./router/register')
// 允许跨域资源共享
app.use(cors());
app.use(express.json());
app.use(loginRouter);
app.use('/api/get', getTitle);
// app.use('/api/qiniu', getQiniuToken); // 指定完整路径
app.use('/api', find);
app.use('/api', cards);
app.use('/api', user);
app.use('/api', code);
app.use('/api', search);
app.use('/api', carouselController)
app.use('/api', comment);
app.use('/api', register);
app.use(express.static(path.join(__dirname, './uploadFile')));
app.use(express.static(path.join(__dirname, './controllers/Carousel')));


app.use((req, res, next) => {
    // 允许加载同源的资源
    res.setHeader("Content-Security-Policy", "default-src 'self'");
    next();
});



// 链接数据库
connectDB();

app.get('/', (req, res) => {
    res.send("这是主页面");
});

// app.post('/api/upload', upload.single('file'), (req, res) => {
//     const fileInfo = {
//         status: 'success',
//         message: 'File uploaded successfully',
//         url: `https://2g59l88703.imdo.co/uploads/${req.query.uid}/${req.file.filename}`
//     };
//     const response = {
//         code: 200,
//         data: fileInfo
//     };
//     console.log("上传成功了", response);
//     res.status(200).json(response)
//     // res.json(response);
// })
app.listen(port, () => {
    console.log(`Server is running on port 127.0.0.1:${port}`);
});
